Ext.define('cloud.controller.Spot', {
    extend: 'Ext.app.Controller',
    requires: ['Ext.picker.Picker'],
    config: {
        file: '',
        filedata: [],
        refs: {
            spot: 'spot',
            spotedit: 'spotedit',
            spotsign: 'spotsign',
            spotadd: 'spotadd',
            spotsign_show: 'spot #spotsign_show',
            spotadd_add: 'spot #spotadd_add',
            spot_upload_icon: 'spot #spot_upload_icon',
            loadedImage: 'spot #loadedImage',
            spotsign_upload_icon: 'spot #spotsign_upload_icon',
            signloadedImage: 'spot #signloadedImage',
            spot_submit: 'spotadd #spot_submit',
            spot_edit_submit: 'spotedit #spot_edit_submit',
            spot_del: 'spotedit #spot_del',
            spot_select: 'spot #spot_select'
        },
        control: {
            /**
             * show categoryadd page
             */
            spotadd_add: {
                tap: function () {
                    var spot = this.getSpot();
                    var spotadd = this.getSpotadd();
                    if (spotadd == null) {
                        spotadd = Ext.create('cloud.view.spot.SpotAdd');
                    }
                    spot.push(spotadd);
                }
            },
            spotsign_show: {
                tap: function () {
                    var spot = this.getSpot();
                    var spotsign = this.getSpotsign();
                    if (spotsign == null) {
                        spotsign = Ext.create('cloud.view.spot.Spotsign');
                    }
                    spot.push(spotsign);
                }
            },
            /**
             * show categoryedit page
             */
            spot_select: {
                itemtap: function (list, item, index, record) {
                    Ext.Viewport.mask({xtype: 'loadmask'});
                    var spot = this.getSpot();
                    var spotedit = this.getSpotedit();
                    if (spotedit == null) {
                        spotedit = Ext.create('cloud.view.spot.SpotEdit');
                    }
                    Ext.getCmp('spot_loadedImage').setSrc(record.getData().image);
                    Ext.getCmp('spotsign_loadedImage').setSrc(record.getData().signimage);
                    Ext.getCmp('spot_id').setValue(record.getData().id);
                    spotedit.setRecord(record);
                    spot.push(spotedit);
                    Ext.Viewport.unmask();
                }
            },
            /**
             * add category
             */
            spot_submit: {
                tap: function () {

                    var name = Ext.getCmp('spot_name').getValue();
                    if (Ext.isEmpty(name)) {
                        Ext.Msg.alert('请输入景点名称');
                        return
                    }
                    if (Ext.isEmpty(Ext.getCmp('spot_display').getValue())) {
                        Ext.Msg.alert('请输入排序');
                        return
                    }
                    Ext.Viewport.mask({xtype: 'loadmask'});
                    this.getSpotadd().submit({
                        url: GLOBAL.api_url + 'spot_add',
                        scope: this,
                        method: 'POST',
                        success: this.executeSuccess
                    });
                    Ext.Viewport.unmask();

                }
            },
            /**
             * edit category
             */
            spot_edit_submit: {
                tap: function () {
                    Ext.Viewport.mask({xtype: 'loadmask'});
                    this.getSpotedit().submit({
                        url: GLOBAL.api_url + 'spot_update',
                        params: {'id': Ext.getCmp('spot_id').getValue()},
                        scope: this,
                        method: 'POST',
                        success: this.executeSuccess
                    });
                    Ext.Viewport.unmask();
                }
            },
            /**
             * delete category
             */
            spot_del: {
                tap: function () {
                    Ext.Msg.confirm('是否删除', '', function (data) {
                        if (data == 'yes') {
                            this.getSpotedit().submit({
                                url: GLOBAL.api_url + 'spot_delete',
                                params: {'id': Ext.getCmp('spot_id').getValue()},
                                scope: this,
                                method: 'POST',
                                success: function (from, result) {
                                    if (result.Variables.Result.code == 0) {
                                        Ext.getStore('SpotStore').load();
                                        this.getSpot().pop();
                                    } else {
                                        Ext.Msg.alert(result.Variables.Message.messagestr);
                                    }
                                }
                            });
                        }
                    }, this);
                }
            },
            spot_upload_icon: {
                loadsuccess: function (dataurl, m, e, file) {
                    var me = this;
                    file['tag'] = 'image';
                    files = this.getFiledata();
                    files.push(file);
                    this.setFiledata(files);
                    var image = me.getLoadedImage();
                    image.setSrc(dataurl);
                }
            },
            spotsign_upload_icon: {
                loadsuccess: function (dataurl, m, e, file) {
                    var me = this;
                    file['tag'] = 'signimage';
                    files = this.getFiledata();
                    files.push(file);
                    this.setFiledata(files);
                    var image = me.getSignloadedImage();
                    image.setSrc(dataurl);
                }
            }
        },
        pagePicker: undefined,
        id: undefined
    },

    executeSuccess: function (from, result) {
        if (result.Variables.Result.code == 0) {
            // 上传图片
            var id = result.Variables.data.id;
            if (Ext.isEmpty(id)) {
                id = Ext.getCmp('spot_id').getValue();
            }
            var result = this.doUpload(this.getFile(), id);

            Ext.getStore('SpotStore').load();
            this.getSpot().pop();
        } else {
            Ext.Msg.alert(result.Variables.Message.messagestr);
        }
    },
    doUpload: function (file, ccid) {
        var me = this;
        var filedata = this.getFiledata();
        Ext.each(filedata, function (file) {
            var http = new XMLHttpRequest();
            var form = new FormData();
            form.append('Filedata', file);
            var url = GLOBAL.api_url + 'spot_attachment_upload&id=' + ccid + '&tag=' + file['tag'];
            if (http.upload && http.upload.addEventListener) {
                // Response handler
                http.onreadystatechange = function (e) {
                    Ext.Viewport.unmask();
                    if (this.readyState == 4) {
                        if (this.status == 200) {
                            var returnText = Ext.decode(this.responseText);
                            if (returnText.Variables.Result.code == 0) {
                                var attachid = returnText.Variables.data.attachid;
                                Ext.getStore('SpotStore').load();  // 产品分类
                                console.log('上传图片ID为 ' + attachid + ' 成功');
                            }
                        }
                    }
                };
                // Error handler
                http.upload.onerror = this.actionfailure;
            }
            http.open('POST', url);
            http.send(form);
        });
        this.setFile('');
    },
    actionfailure: function () {//请求失败时执行操作
        Ext.Viewport.unmask();
        Ext.Msg.alert('请求失败', '服务器维护中....');
    }

});